//显示中英文字符程序

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define LOGO16_GLCD_HEIGHT 16 //定义显示高度
#define LOGO16_GLCD_WIDTH  16 //定义显示宽度
#define buzzer 2              //初始化引脚
#define L_VRx A0
#define L_VRy A1
#define L_VRz 4
#define R_VRx A6
#define R_VRy A7
#define R_VRz 3
#define Button1 5
#define Button2 6
#define RED 12
#define Control_num 8
SoftwareSerial blueTooth(10, 11);
//摇杆部分变量
int Sweep;
int Sweep3;

int Sweep1;
int Sweep2;

int flag1 = 0;
int flag2 = 0;
int flag3 = 0;

int L_RockerZ;
int R_RockerZ;
int turn_flag = 1;
int count = 0;
int num = 1; //默认控制关节
//按钮部分变量
int Btn1;
int Btn2;

//蓝牙部分变量
char BT_data;
char BT_num;
char BT_x;
char BT_y;
char BT_z;
char BT_b1='1';
char BT_b2='1';
//音频部分变量
#define NTC0 -1
#define NTC1 262
#define NTC2 294
#define NTC3 330
#define NTC4 350
#define NTC5 393
#define NTC6 441
#define NTC7 495
#define NTCL5 196
#define NTCL6 221
#define NTCL7 248
#define NTCH1 525


int length;
int tonepin = 6;
int M_flag = 1;
void frequence_1(void)    // 500 HZ
{
  int i ;
  for (i = 0; i < 30; i++)
  {
    digitalWrite(buzzer, HIGH); //sound production
    delay(1);
    digitalWrite(buzzer, LOW);
    delay(1);
  }
}

void frequence_2(void)    // 250 HZ
{
  int i ;
  for (i = 0; i < 30; i++)
  {
    digitalWrite(buzzer, HIGH); //sound production
    delay(2);
    digitalWrite(buzzer, LOW);
    delay(2);
  }
}

int Value(int sweep)
{
  int flag = 0;
  if (sweep > 1000)
  {
    flag = 1;
  }
  else if (sweep < 100)
  {
    flag = 2;
  }
  else
  {
    flag = 0;
  }

  return flag;
}

void Blink()
{
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(100);
}
void setup()   {
  Serial.begin(115200);
  blueTooth.begin(9600);//设置软串口通讯波特率
  pinMode(buzzer, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  //  pinMode(L_VRz,OUTPUT);
  //  pinMode(R_VRz,OUTPUT);
  pinMode(RED, OUTPUT);
  pinMode(L_VRz, INPUT_PULLUP);
  pinMode(R_VRz, INPUT_PULLUP);
  pinMode(Button1, INPUT_PULLUP);
  pinMode(Button2, INPUT_PULLUP);
  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done

  display.clearDisplay(); //清屏

  //英文字符显示，直接用display.println或print显示字符串就行
  //println换行，print不换行
  //  display.setTextSize(1);             //设置字体大小
  display.setTextColor(WHITE);        //设置字体颜色白色
  display.setCursor(2, 2);            //设置字体的起始位置
  display.println("Hello");   //输出字符并换行
  display.println("Welcome:");          //输出数字并换行
  display.display();                  //把缓存的都显示
  frequence_2();
  delay(100);
  frequence_2();
  delay(100);
  frequence_2();
  delay(100);

//  length = sizeof(tune) / sizeof(tune[0]);

}

void loop() {



  Sweep = analogRead(L_VRx);
  Sweep3 = analogRead(L_VRy);


  Sweep1 = analogRead(R_VRx);
  Sweep2 = analogRead(R_VRy);
  L_RockerZ = digitalRead(L_VRz);
  R_RockerZ = digitalRead(R_VRz);
  flag1 = Value(Sweep1);
  flag2 = Value(Sweep2);
  flag3 = Value(Sweep3);
  Btn1 = digitalRead(Button1);
  Btn2 = digitalRead(Button2);


  if (Btn1 == 0)
  {
    digitalWrite(RED, HIGH);
    BT_b1='0';
  }
  else if (Btn2 == 0)
  {
    digitalWrite(RED, HIGH);
    BT_b2='0';
  }
  else
  {
    digitalWrite(RED, LOW);
    BT_b1='1';
    BT_b2='1';
    
  }




  if (R_RockerZ == 0 && turn_flag == 1)
  {
    frequence_1();
    Blink();
    if (num >= Control_num)
    {
      num = 1;
    }
    else
    {
      num += 1;
    }
    turn_flag = 0;

  }
  if (L_RockerZ == 0 && turn_flag == 1)
  {
    frequence_1();
    Blink();
    if (num <= 1)
    {
      num = Control_num;
    }
    else
    {
      num -= 1;
    }
    turn_flag = 0;
  }


  count++;
  if (count > 1)
  {
    count = 0;
    turn_flag = 1;
  }





  display.clearDisplay(); //清屏

  //英文字符显示，直接用display.println或print显示字符串就行
  //println换行，print不换行
  //  display.setTextSize(1);             //设置字体大小
  display.setTextColor(WHITE);        //设置字体颜色白色
  display.setCursor(0, 0);            //设置字体的起始位置
  display.println("Monitor:");   //输出字符并换行
  display.print("Current Joint:");
  display.print(" ");
  display.println(num);          //输出数字并换行
  display.setCursor(2, 16);
  display.print("Swp1");
  display.setCursor(47, 16);
  display.print("Swp2");
  display.setCursor(92, 16);
  display.print("Swp3");

  display.setCursor(8, 25);
  if (flag1 == 0)
  {
    display.print('0');
    BT_x='0';
  }
  else if (flag1 == 1)
  {
    display.print('+');
    BT_x='+';
    frequence_2();
  }
  else if (flag1 == 2)
  {
    display.print('-');
    BT_x='-';
    frequence_2();
  }

  display.setCursor(53, 25);

  if (flag2 == 0)
  {
    display.print('0');
    BT_y='0';
  }
  else if (flag2 == 1)
  {
    display.print('+');
    BT_y='+';
    frequence_2();
  }
  else if (flag2 == 2)
  {
    display.print('-');
    BT_y='-';
    frequence_2();
  }

  display.setCursor(96, 25);
  if (flag3 == 0)
  {
    display.print('0');
    BT_z='0';
  }
  else if (flag3 == 1)
  {
    display.print('+');
    BT_z='+';
    frequence_2();
    
  }
  else if (flag3 == 2)
  {
    display.print('-');
    BT_z='-';
    frequence_2();
  }

  display.display();                  //把缓存的都显示
  
//蓝牙发送

  blueTooth.print('n');
  BT_num=num+'0';
  blueTooth.print(BT_num);
  blueTooth.print('x');
  blueTooth.print(BT_x);
  blueTooth.print('y');
  blueTooth.print(BT_y);
  blueTooth.print('z');
  blueTooth.print(BT_z);
  blueTooth.print('w');
  blueTooth.print(BT_b1);
  blueTooth.print('s');
  blueTooth.print(BT_b2);
  blueTooth.print('d');
  blueTooth.println('0');

  Serial.print('n');
  Serial.print(BT_num);
  Serial.print('x');
  Serial.print(BT_x);
  Serial.print('y');
  Serial.print(BT_y);
  Serial.print('z');
  Serial.print(BT_z);
  Serial.print('w');
  Serial.print(BT_b1);
  Serial.print('s');
  Serial.print(BT_b2);
  Serial.print('d');
  Serial.println('0');

  
//  BT_data = blueTooth.read();
//  if (BT_data > 0) {
//    Serial.println(BT_data);
//  }
  delay(200);
}
